package com.atguigu.common.exception;

import com.atguigu.common.result.ResponseEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 自定义异常   目标：使用一个或较少的异常类，可以捕获和显示所有的异常信息
 * 因此，先创建一个自定义异常类(必须是RuntimeException)，
 * 在程序中抛出这个自定义异常对象
 * 并在统一异常处理器中捕获自定义异常
 */
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException {
    private Integer code;
    private String message;

    //错误消息
    public BusinessException(String message) {
        this.message = message;
    }

    //    错误消息+错误码
    public BusinessException(String message, Integer code) {
        this.message = message;
        this.code = code;
    }

    //    错误消息+错误码+原始异常对象
    public BusinessException(String message, Integer code, Throwable cause) {
        super(cause);
        this.message = message;
        this.code = code;
    }

    //    接收枚举类
    public BusinessException(ResponseEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    //    接收枚举类+原始异常对象
    public BusinessException(ResponseEnum resultEnum, Throwable cause) {
        super(cause);
        this.message = resultEnum.getMessage();
        this.code = resultEnum.getCode();
    }
}
